package com.example.interviewrecorder.components

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.constraintlayout.widget.ConstraintSet
import com.example.interviewrecorder.LyyApplicaiton
import com.example.interviewrecorder.R
import com.example.interviewrecorder.databinding.DialogLyyBinding
import com.example.interviewrecorder.utils.LyyActivityCollector

class LyyDialog(
    context: Context,
    var title: String = LyyApplicaiton.context.getString(R.string.title),
    var content: String = LyyApplicaiton.context.getString(R.string.dialog_content),
    var clickDismiss: Boolean = true,
    var onNegativeBtnClick: (() -> Unit)? = null,
    var onPositiveBtnClick: (() -> Unit)? = null,
    //若是tip，则没有取消按钮
    var isTip: Boolean = false
) : Dialog(context) {
    private val binding: DialogLyyBinding = DialogLyyBinding.inflate(LayoutInflater.from(context))

    init {
        setContentView(binding.root)
        binding.titleTv.text = title
        binding.contentTv.text = content
        if (isTip) {
            binding.negativeBtn.visibility = View.GONE
            binding.guideline.visibility = View.GONE
            binding.verticalDivider.visibility = View.GONE

            val constraintSet = ConstraintSet()
            constraintSet.clone(binding.root)
            constraintSet.connect(
                R.id.positive_btn, ConstraintSet.START, ConstraintSet.PARENT_ID,
                ConstraintSet.START
            )
            constraintSet.applyTo(binding.root)
        }
        binding.negativeBtn.setOnClickListener {
            onNegativeBtnClick?.invoke()
        }
        binding.positiveBtn.setOnClickListener {
            onPositiveBtnClick?.invoke()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //给弹框设置圆角背景
        window?.setBackgroundDrawableResource(R.drawable.lyy_dialog_bg)

        //设置弹框的宽度为屏幕的80%
        window?.setLayout((getScreenWidth() * 0.8).toInt(), WindowManager.LayoutParams.WRAP_CONTENT)
    }

    private fun getScreenWidth(): Int {
        return context.resources.displayMetrics.widthPixels
    }
}

/**
 * 显示有两个按钮的弹框提示
 */
fun showInfo(
    title: String = LyyApplicaiton.context.getString(R.string.title),
    content: String = LyyApplicaiton.context.getString(R.string.dialog_content),
    clickDismiss: Boolean = true,
    onNegativeBtnClick: (() -> Unit)? = null,
    onPositiveBtnClick: (() -> Unit)? = null
) {
    val dialog = LyyDialog(
        context = LyyActivityCollector.getCurrActivity(),
        title = title,
        content = content,
        clickDismiss = clickDismiss
    )
    if (onNegativeBtnClick == null) {
        //取消按钮默认为关闭弹框
        dialog.onNegativeBtnClick = {
            if (clickDismiss) {
                dialog.dismiss()
            }
        }
    } else {
        dialog.onNegativeBtnClick = {
            onNegativeBtnClick()
            if (clickDismiss) {
                dialog.dismiss()
            }
        }
    }

    //确认按钮默认为关闭弹框
    if (onPositiveBtnClick == null) {
        dialog.onPositiveBtnClick = {
            if (clickDismiss) {
                dialog.dismiss()
            }
        }
    } else {
        dialog.onPositiveBtnClick = {
            onPositiveBtnClick()
            if (clickDismiss) {
                dialog.dismiss()
            }
        }
    }
    dialog.show()
}


/**
 * 显示只有一个按钮的弹框提示
 */
fun showTip(
    title: String = LyyApplicaiton.context.getString(R.string.tip_title),
    content: String = LyyApplicaiton.context.getString(R.string.dialog_content),
    clickDismiss: Boolean = true,
    onPositiveBtnClick: (() -> Unit)? = null
) {
    val dialog = LyyDialog(
        context = LyyActivityCollector.getCurrActivity(),
        title = title,
        content = content,
        clickDismiss = clickDismiss,
        isTip = true
    )

    //确认按钮默认为关闭弹框
    if (onPositiveBtnClick == null) {
        dialog.onPositiveBtnClick = {
            if (clickDismiss) {
                dialog.dismiss()
            }
        }
    } else {
        dialog.onPositiveBtnClick = {
            onPositiveBtnClick()
            if (clickDismiss) {
                dialog.dismiss()
            }
        }
    }
    dialog.show()
}